<template>
  <div class='cardBom'>
    <el-button @click="printing" style='margin:10px 0' >打印运单</el-button>
    <el-table
      border
      :data="$store.state.retailers.lowerData||[]"
      highlight-current-row
      height='300'
      ref='table'
      style="width: 100%"
      @selection-change="handleSelectionChange"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="55" align='center'/>
      <el-table-column prop="orderNum" label="拣货顺序" align='center'></el-table-column>
      <el-table-column prop="code" label="订单号" align='center' width='150'></el-table-column>
      <el-table-column prop="isvCode" label="ISV单号" align='center' width='150'></el-table-column>
      <el-table-column prop="status" label="状态" :formatter="formatterStatus" align='center' ></el-table-column>
      <el-table-column prop="goodsSortQuantity" label="商品种数" align='center'></el-table-column>
      <el-table-column prop="pickNum" label="商品数量" align='center'></el-table-column>
      <el-table-column prop="recheckQuantity" label="复核数量" align='center'></el-table-column>
      <el-table-column label="包材" align='center'>
        <template slot-scope="scope">
          <el-select v-model="scope.row.packingMaterialCode"
          :disabled='state.modelPrintFlag'
           @change='value=>materialChange(value,scope.row,scope.row.packingMaterialList)' placeholder="包材">
            <el-option
              v-for="item in scope.row.packingMaterialList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-dialog :visible.sync="$store.state.retailers.modelPrintFlag" destroy-on-close width="600" height="600"  title='打印'>
      <printFaceSheet   @fatherPrintMethod="fatherPrint" :printData="$store.state.retailers.printData" :parcelType="$store.state.retailers.parcelType"/>
    </el-dialog> -->
  </div>

</template>

<script>
import soonPrint from '@/components/Printing/printBefore.js'
import request from '@/utils/request'
export default {
  name:'cardBom',
  // components:{
  //   printFaceSheet
  // },
 data() {
    return {
      arrData:[],
      hasSelectList:[1],
      printData:[],//打印的数据
      parcelType:'',//哪家快递
      state:this.$store.state.retailers,
      allAlign:'center',
      options:[]
    };
  },
  created(){
    this.$store.commit('retailers/SET_CELLBACK',this.setCheckboxRow)
    // request({
    //   url:'/wms/packingMaterial/queryListByVolumeForRegularReview',
    //   method:'get'
    // }).then(res=>{

    // })
  },
  destroyed() {
    this.$eventBus.$off('printFinish')

  },
  mounted() {
    // this.$eventBus.$on("printFinish", () => {
    //   // Do something
    //   console.log('打印完成')
    //   this.$store.commit('retailers/SET_FLAG',false)
    // })

  },
  methods: {
    //baocai
    materialChange(val,row,list){
      let obj = list.find(v=>v.code === val)
      let data = {
        deliveryCode:row.code,
        packingMaterialCode:obj.code,
        packingMaterialName:obj.name,
        volume:obj.volume
      }
      request({
        url:'/wms/packingMaterialGoods/saveAndGenRecordForGeneralReview',
        method:'post',
        data
      }).then(res=>{
        this.$message.success(res.msg)
      })
    },
    //多选
    handleSelectionChange(val){
      this.arrData = val
    },
    //单选
    handleCurrentChange(){
      this.$refs.table.setCurrentRow(this.$store.state.retailers.lowerData[0]);
    },
    //设置默认选中
    setCheckboxRow(){
      this.$refs.table.setCurrentRow(this.$store.state.retailers.lowerData[0], true)
    },
    //传给store打印点击
    fatherPrint(){
      this.$store.commit('retailers/SET_FLAG',false)
    },
    //状态
    formatterStatus(row, column, cellValue, index ){
      if(cellValue  === '0')return '新增'
      if(cellValue  === '1')return '待分配'
      if(cellValue  === '2')return '部分分配'
      if(cellValue  === '3')return '已分配'
      if(cellValue  === '4')return '已作废'
      if(cellValue  === '5')return '生成波次中'
      if(cellValue  === '6')return '待拣货'
      if(cellValue  === '7')return '待复核'
      if(cellValue  === '8')return '复核中'
      if(cellValue  === '9')return '待发货'
      if(cellValue  === '10')return '发货中'
      if(cellValue  === '11')return '出库完成'
    },
    //打印运货单
    printing(){
      const arr = this.arrData;
      if(arr.length === 0)return this.$message.error('请选择要打印的数据')
      let array = [];
      let expressType = [];
      let oddNumbers=[]
      arr.forEach((i) => {
        if ((i.logisticsCode   && i.logisticsCode   !== '') && (i.waybillCode && i.waybillCode !== '')) {
          array.push(i.code);
          expressType.push(i.logisticsCode);
        } else {
          let v = `单号${i.code}`
          oddNumbers.push(v)
        }
      });
      console.log(oddNumbers,array,expressType)
      if (oddNumbers.length>0) {
        this.$message.error('请选择带有承运商和运单号的单据来进行打印 ')
        return false
      }
      if (expressType.length>0) {
        let array = [...new Set(expressType)]
        if (array.length>1) {
          this.$message.error('批量打印同时只能打印同一家快递！')
          return false
        }
      }
      this.$store.commit('retailers/SET_PRCELTYPE',expressType[0])
      localStorage.setItem('printCode', JSON.stringify(array))
      let routeUrl = this.$router.resolve({
        path: '/printResult',
        query: {
          type: 'array',
          listInterface: 'printExpress'
        }
      })
      // window.open(routeUrl.href, '_blank')
      soonPrint(routeUrl.href)
      // printExpress(array).then(res => {{
      //   if (res.status === 0&&res.data.length) {
      //       new PrintWaybill({ printData: res.data }).initSocket()
      //   }else {
      //       this.$message.warning('没有打印数据不可打印！')
      //     }
      // }})
    },
    //选中
    radioChangeEvent ({ row,rowIndex }) {
      this.radioValue = rowIndex
      this.$store.dispatch("retailers/getTopData",row.data)
    },
  }
}
</script>

<style>
</style>
